package tgc.edu.exam.web.controller;

import java.util.HashSet;
import java.util.UUID;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import tgc.edu.exam.custom.CommonController;
import tgc.edu.exam.entity.EvaluationXz;
import tgc.edu.exam.entity.EvaluationXz;
import tgc.edu.exam.service.EvaluationXzService;
import tgc.edu.exam.web.form.EvaluationXzForm;

@Controller
@RequestMapping(value = "evaluationXz")
public class EvaluationXzController extends CommonController<EvaluationXz, UUID, EvaluationXzForm>{
	@Autowired
	private EvaluationXzService eservice;

	@Override
	public Specification<EvaluationXz> buildSpec(EvaluationXzForm form) {
		Specification<EvaluationXz> specification = new Specification<EvaluationXz>() {

			private static final long serialVersionUID = 1L;
			
			@Override
			public Predicate toPredicate(Root<EvaluationXz> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				HashSet<Predicate> rules=new HashSet<>();
				if(StringUtils.hasText(form.getType())) {
					Predicate name = cb.like(root.get("type"), "%"+form.getType()+"%");
					rules.add(name);
				}
				if(StringUtils.hasText(form.getExplain())) {
					Predicate name = cb.like(root.get("explain"), "%"+form.getExplain()+"%");
					rules.add(name);
				}
				if(StringUtils.hasText(form.getSystem())) {
					Predicate name = cb.like(root.get("system"), "%"+form.getSystem()+"%");
					rules.add(name);
				}
				if(form.getWeight()!=null) {
					Predicate name = cb.like(root.get("weight"), "%"+form.getWeight()+"%");
					rules.add(name);
				}
				return cb.and(rules.toArray(new Predicate[rules.size()]));
			}

		};
		return specification;
	}
	
	
}
